use std::{sync::Arc};

use rspc::{Config, Type};
use serde::Serialize;

use crate::{prisma::{PrismaClient}};

pub type Router = rspc::Router<Ctx>;
pub(crate) type RouterBuilder = rspc::RouterBuilder<Ctx>;

pub struct Ctx {
    pub client: Arc<PrismaClient>,
}

use utils::{InvalidRequests};

mod opinion;
mod config;
mod record;
mod meeting;
mod rsync;
mod handle;
mod action;
mod all;

pub mod utils;

pub(crate) fn mount() -> Arc<Router> {
    let config = Config::new().set_ts_bindings_header("/* eslint-disable */");

    let config = config.export_ts_bindings(
		std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../packages/client/src/core.ts"),
	);

    let r = <Router>::new()
        .config(config)
        .query("buildInfo", |t| {
			#[derive(Serialize, Type)]
			pub struct BuildInfo {
				version: &'static str,
			}

			t(|_, _: ()| BuildInfo {
				version: "0.0.1",
			})
		})
        .merge("configs.", config::mount())
        .merge("opnions.", opinion::mount())
        .merge("records.", record::mount())
        .merge("meetings.", meeting::mount())
        .merge("rsyncs.", rsync::mount())
        .merge("all.", all::mount())
        .merge("handle.", handle::mount())
        .merge("actions.", action::mount())
        .build()
        .arced();

    InvalidRequests::validate(r.clone());

    r
}
